import { getVideBackListPage, getSlideVideoBack, getVideoGreatVideoPage, getLiveBackSlide } from '@/server/news'
import Page from '@/components/Layout'
import GameVideo from '@/components/GameVideo'
import VideoTape from '@/components/VideoTape'
import { videoBackNavList } from '@/common/enum'
import { getLiveBackDetail, getShortVideoDetail } from '@/server/news'
const Index = (props) => props?.isVideoTape ? <VideoTape {...props} /> : <GameVideo {...props}></GameVideo>
Index.getLayout = (page, props) => {
  return (
    <Page header footer {...props}>
      {page}
    </Page >
  )
}

/**
 * 参数1 赛事拼音 
 * 参数2 带s的分页列表 
 * 
 * 参数1 赛事拼音
 * 参数2 不带s的是数字尝试直接播放
 * 
 * 参数1 是数字并且没有参数参数2 直接尝试播放 
 *
*/
export async function getServerSideProps (ctx) {
  let name = ctx?.params?.id?.[0] || videoBackNavList?.f?.[0]?.en//类别的拼音名称 有可能直接是一个播放id
  //播放的页面的条件  两个参数 并且 第二节参数的不是s开头 或者 第一个参数完全是数字
  const checkNumber = (value) => (!isNaN(parseFloat(value)) && isFinite(value))

  if ((ctx?.params?.id?.length > 1 && !ctx?.params?.id?.[1]?.includes('s')) || (checkNumber(Number(name)) && ctx?.params?.id?.length === 1)) {
    //录像回放的页面
    let id = ctx?.params?.id?.[1]
    if (checkNumber(Number(name)) && ctx?.params?.id?.length === 1) id = Number(name)
    const type = '1'//这个地方只有录像
    let api = {
      "1": getShortVideoDetail,
      "2": getLiveBackDetail
    }
    const slideApi = {
      "1": getSlideVideoBack, //端视频
      "2": getLiveBackSlide,//回放
    }
    const res = await Reflect.get(api, type)(id)
    const slideParams = {
      "1": {
        num: 3,
        type: 4,
        typeId: res.typeId
      },
      "2": {
        num: 3,
        typeId: res.typeId,
        type: 1,
      }
    }


    const bottomApi = {
      "1": getSlideVideoBack, //短视频
      "2": getLiveBackSlide,//回放
    }
    const bottomParams = {
      "1": {
        num: 8,
        type: 4,
        typeId: res.typeId,
      },
      "2": {
        num: 8,
        typeId: res.typeId,
        type: 4,
      }
    }
    const [slideData, bottomData] = await Promise.all([Reflect.get(slideApi, type)(Reflect.get(slideParams, type)), Reflect.get(bottomApi, type)(Reflect.get(bottomParams, type))])
    return {
      props: {
        videoInfo: res,
        id,
        slideData,
        bottomData,
        isVideoTape: true,
        en: name,
        pageLink: "marvellous",
      }
    }
  } else {
    const videoType = '1'//  1 视频 2录像
    const currentInfo = Object.values(videoBackNavList).flat(1).find(r => r.en === name)
    let id = currentInfo?.id || 10
    const params = {
      pageNum: ctx?.params?.id?.[1]?.split('s')[1] || 1, //分页
      pageSize: 6,
      id,//赛事id
      en: name,
    }

    const api = {
      "1": getVideoGreatVideoPage,
      "2": getVideBackListPage
    }

    const slideApi = {
      "1": getSlideVideoBack,
      "2": getLiveBackSlide
    }
    const p = {
      "1": { num: 6, type: 3 },
      "2": { num: 6, type: 4 }
    }
    const [videoList, videoData] = await Promise.all([Reflect.get(slideApi, videoType)(Reflect.get(p, videoType)), Reflect.get(api, videoType)({ pageSize: params.pageSize, pageNum: params.pageNum, typeId: params.id }),])
    params.total = videoData.total
    params.current = videoData.current
    return {
      props: {
        params,
        videoData: videoData?.records,
        videoList,
        videoType,
        isVideoTape: false
      }
    }
  }
}

export default Index